<template>
  <div class="base-map-resource-content">
    <div class="query-content">
      <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline backend-form"
      >
        <el-form-item label="角色名称">
          <el-input v-model="formInline.roleName" @change="onSubmit" />
        </el-form-item>
        <!--        <el-form-item>-->
        <!--          <el-button type="primary" class="queryButton" @click="onSubmit"-->
        <!--            >查询</el-button-->
        <!--          >-->
        <!--          <el-button type="primary" class="resetButton" @click="resetField"-->
        <!--            >重置</el-button-->
        <!--          >-->
        <!--        </el-form-item>-->
        <el-form-item class="backend-button">
          <el-button
            class="addButton"
            type="primary"
            @click="handleAddRole"
            v-if="currentUser?.hasPermission('jsgladd')"
          >
            <!--        <el-icon>-->
            <!--          <Plus />-->
            <!--        </el-icon>-->
            <i class="icon el-icon-plus"></i>
            {{ currentUser?.getButtonName('jsgladd') }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-content">
      <div class="table">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column align="center" label="序号" width="180">
            <template #default="scope">
              {{
                scope.$index +
                1 +
                formInline.pageSize * (formInline.pageIndex - 1)
              }}
            </template>
          </el-table-column>
          <el-table-column label="角色名称" show-overflow-tooltip>
            <template #default="scope">
              <span class="userName">
                {{ scope.row.roleName }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="角色描述"
            prop="remark"
            show-overflow-tooltip
          />
          <el-table-column
            prop="createTime"
            label="创建时间"
            show-overflow-tooltip
          >
            <template #default="scope">
              {{
                scope.row.createDate
                  ? formatDate(
                      new Date(scope.row.createDate),
                      'YYYY-MM-DD HH:mm:ss'
                    )
                  : ''
              }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <template v-if="scope.row.pkid !== 'administrator'">
                <el-button
                  text
                  type="text"
                  style="padding: 0"
                  @click="handleEditClick(scope.row)"
                  v-if="currentUser?.hasPermission('jsgledit')"
                >
                {{ currentUser?.getButtonName('jsgledit') }}
                </el-button>
                <el-popconfirm
                  width="160px"
                  confirm-button-text="确认"
                  cancel-button-text="取消"
                  icon-color="#626AEF"
                  @confirm="handleDelete"
                  :title="'确认删除用户' + scope.row.roleName + '吗？'"
                >
                  <template #reference>
                    <el-button
                      text
                      type="text"
                      style="color: #fa3434; margin-left: 12px"
                      @click="handleDeleteClick(scope.row)"
                      v-if="currentUser?.hasPermission('jsgldelete')"
                      >{{
                        currentUser?.getButtonName('jsgldelete')
                      }}</el-button
                    >
                  </template>
                </el-popconfirm>
              </template>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        background
        layout="slot,prev, pager, next"
        :page-size="formInline.pageSize"
        :total="total"
        @current-change="handlePageChange"
        :current-page="formInline.pageIndex"
      >
        <template #default> 共{{ total }}个角色 </template>
      </el-pagination>
    </div>
    <add-role-dialog
      :role-info="roleInfo"
      :visible="dialogVisible"
      @updateRole="handleRoleUpdate"
      @closeDialog="handleDialogClose"
    ></add-role-dialog>
    <!--    <el-dialog-->
    <!--      :visible.sync="deleteVisible"-->
    <!--      title="提示"-->
    <!--      align-center-->
    <!--      class="outline-dialog"-->
    <!--      width="30%"-->
    <!--    >-->
    <!--      <span>确认删除角色“{{ roleInfo.roleName }}”吗？</span>-->
    <!--      <template #footer>-->
    <!--        <div class="dialog-footer">-->
    <!--          <el-button @click="handleDeleteCloseCanel">取消</el-button>-->
    <!--          <el-button type="primary" @click="handleDelete"> 确认 </el-button>-->
    <!--        </div>-->
    <!--      </template>-->
    <!--    </el-dialog>-->
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, getCurrentInstance } from 'vue'
// import { Plus } from '@element-plus/icons-vue'
import AddRoleDialog from '@/views/backend-management/components/role/components/addRoleDialog.vue'
import { formatDate } from '@/utils'
import { deleteRole, queryRoleList } from '@/api/back-management/role'
import type { RoleDataResp } from '@/api/back-management/role'
import { Message as ElMessage } from 'element-ui'
// import { useCurrentUserStore } from '@/stores/current-user'
import { PERMISSIONS } from '@/common/user'
// import { currentUser } from '@/common/user'

const vm = getCurrentInstance()
const store = vm?.proxy?.$store

const currentUser = store?.state?.user?.currentUser

// const { currentUser } = useCurrentUserStore()

const dialogVisible = ref(false)
const deleteVisible = ref(false)

const formInline = reactive({
  pageIndex: 1,
  pageSize: 11,
  roleName: '',
})
const tableData = ref<RoleDataResp[]>([])
const total = ref(0)

const roleInfo = reactive<RoleDataResp>({
  pkid: '',
  roleName: '',
})

const getRoleList = () => {
  queryRoleList(formInline).then((res) => {
    if (res.success) {
      tableData.value = res.data.list
      total.value = res.data.count
    }
  })
}

onMounted(() => {
  getRoleList()
})

const onSubmit = () => {
  formInline.pageIndex = 1
  getRoleList()
}

const resetField = () => {
  formInline.pageIndex = 1
  formInline.pageSize = 11
  formInline.roleName = ''
  getRoleList()
}

const handleEditClick = (item: RoleDataResp) => {
  Object.assign(roleInfo, {
    remark: item.remark,
    roleName: item.roleName,
    pkid: item.pkid,
    roleLevel: item.roleLevel,
  })
  dialogVisible.value = true
}

const handleDeleteClick = (item: RoleDataResp) => {
  Object.assign(roleInfo, {
    remark: item.remark,
    roleName: item.roleName,
    pkid: item.pkid,
  })
  deleteVisible.value = true
}

const handleDelete = () => {
  deleteRole(roleInfo.pkid)
    .then((res) => {
      console.log(res)
      if (res.success) {
        handleRoleUpdate()
        ElMessage.success('删除角色成功！')
      } else {
        ElMessage.error(res.message)
      }
    })
    .catch((e) => {
      ElMessage.error(e)
    })
    .finally(() => {
      deleteVisible.value = false
    })
}

const handleAddRole = () => {
  dialogVisible.value = true
}

const handleDialogClose = () => {
  dialogVisible.value = false
  Object.assign(roleInfo, {
    roleName: '',
    remark: '',
    roleLevel: '3',
    status: '1',
    pkid: '',
  })
}

const handleRoleUpdate = () => {
  handleDialogClose()
  onSubmit()
}

const handleDeleteCloseCanel = () => {
  deleteVisible.value = false
  Object.assign(roleInfo, {
    roleName: '',
    remark: '',
    roleLevel: '3',
    status: '1',
    pkid: '',
  })
}

const handlePageChange = (pageIndex: number) => {
  formInline.pageIndex = pageIndex
  getRoleList()
}
</script>

<style lang="scss" scoped>
.base-map-resource-content {
  width: 100%;
  height: calc(100% - 27px);
  background-color: white;
  box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.1);
  border-radius: 2px;

  .query-content {
    padding-left: 20px;
    width: 100%;
    height: 80px;
    line-height: 80px;
    border-bottom: 1px solid #eeeff5;

    :deep(.el-form) {
      .el-form-item {
        margin-bottom: 0;

        .resetButton {
          width: 80px;
          height: 32px;
          color: #28b1e4;
          background: #ffffff;
          border: 1px solid #28b1e4;
          border-radius: 2px;
        }

        .queryButton {
          width: 80px;
          height: 32px;
          background: #28b1e4;
          border-radius: 2px;
        }
      }
    }
  }
  .addButton {
    margin-left: 20px;
    margin-top: 14px;
    margin-bottom: 14px;
    width: 100px;
    height: 32px;
    //background: #28b1e4;
    border-radius: 2px;
    //border: none;
    padding: 0;
  }
  .table-content {
    width: 100%;
    height: calc(100% - 63px);

    .table {
      width: 100%;
      padding: 0 18px;
      height: calc(100% - 77px - 32px - 28px);
      overflow-y: auto;

      .userName {
        .item {
          margin-left: 6px;
        }

        :deep(.el-icon) {
          vertical-align: sub;

          .svg-icon {
            svg {
              width: 16px;
              height: 16px;
              color: #28b1e4;
            }
          }
        }

        :deep(.el-badge__content) {
          width: 12px;
          height: 13px;
          transform: translateY(85%) translateX(148%);
        }
      }

      :deep(.el-table__row--striped) {
        background: #f7f9fb;
      }
    }

    .el-pagination {
      margin-top: 15px;
      justify-content: end;
      margin-right: 18px;
    }
  }
}
</style>
